package com.apes.framework.plugin.workflow.parser.element;

import com.apes.framework.plugin.filereader.xml.ApesElement;
import com.apes.framework.plugin.filereader.xml.ApesElementParser;
import com.apes.framework.plugin.workflow.parser.model.Group;
import com.apes.framework.plugin.workflow.parser.model.User;
import com.apes.framework.plugin.workflow.parser.node.WorkflowNodeParser;
import com.apes.framework.util.LogicalUtil;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 功能：users元素解析器
 *
 * @author xul
 * @create 2018-06-30 11:35
 */
@Component("workflow.GroupElementParser")
public class GroupElementParser implements ApesElementParser<ApesElement, Group> {

    /**
     * 可解析的元素列表
     **/
    private final String[] elementNames = {"group"};

    /**
     * 功能：可解析的节点或元素列表
     */
    @Override
    public String[] getNames() {
        return elementNames;
    }

    /**
     * 功能：可解析节点域
     */
    @Override
    public String getDomain() {
        return WorkflowNodeParser.domain;
    }

    /**
     * 功能：元素解析
     *
     * @param element
     */
    @Override
    public Group parse(ApesElement element) {
        Group group = new Group();
        group.setTest(StringUtils.isEmpty(element.getAttribute("test")) ? "1 == 1" : element.getAttribute("test"));
        List<User> users = new ArrayList();
        element.getNestedElements().stream().filter(e -> LogicalUtil.in(e.getName(), "role", "user", "post")).forEach(e -> users.add(e.parse()));
        group.setUsers(users);
        return group;
    }
}
